package enemyObject;

import com.sxt.AnimationObj;
import OwnSideObject.BulletObj;
import com.sxt.GameMusic;
import com.sxt.BacGroundObj;
import com.sxt.GameObject;
import com.sxt.GameWin;

import java.awt.*;

/**
 * 地方战斗机
 */

public class EnemyObj extends EnemyObject{
    public static int ENEMY_1_WIDTH = 57; //一号敌机的宽度
    public static int ENEMY_1_HEIGHT = 43; //一号敌机的高度
    //敌方1号飞机爆炸效果动画图片张数
    public static int AnimationNums = 4;
    //敌方1号飞机爆炸动画效果图片路径及格式
    public static String AnimationPath = "images/enemy1_down";
    public static String PicFormat = ".png";
    public int initialLife = 1;
    public int life = initialLife;
    public EnemyObj() {
        super();
    }

    public EnemyObj(String image, GameWin frame) {
        super(image, frame);
    }

    public EnemyObj(String image, int x, int y, int speedY, GameWin frame,int flag) {
        super(image, x, y, speedY, frame,flag);
        this.score=2;
    }

    @Override
    public void paintSelf(Graphics s) {
        //动画替身
        if (this.flag==2){
            this.Frame.animationObjs.add(new AnimationObj(AnimationPath, PicFormat,AnimationNums,this.x,this.y,this.Frame));
            this.Frame.removeList.add(this);
        }

        s.drawImage(image,x,y,null);
        y+=speedY;
        //越界的敌方飞机删除
        if(y>GameWin.WINDOW_HEIGHT){
            y=GameWin.ENEMY_VANISH_LOCATION_Y;
            x=GameWin.ENEMY_VANISH_LOCATION_X;
            this.Frame.removeList.add(this);
        }
        //画出敌机血条
        s.setColor(Color.RED);
        s.drawRect(x, y - 10, ENEMY_1_WIDTH, 5);
        s.fillRect(x, y - 10, (int)(ENEMY_1_WIDTH * ((double)life / initialLife)), 5);
        //敌方飞机与我方子弹碰撞检测
        if (!this.Frame.bulletObjList.isEmpty())
            for (BulletObj bulletObj:this.Frame.bulletObjList){
                if (this.getRec().intersects(bulletObj.getRec())){
                    //子弹图片不显示后，如果不改变坐标，仍然会和其他飞机进行碰撞检测，从而影响到其他飞机
                    GameMusic.enemy1_down.play();
                    //敌机爆炸动画
                    life--;
                    if(life <= 0){
                        this.Frame.animationObjs.add(new AnimationObj(AnimationPath, PicFormat,AnimationNums,this.x,this.y,this.Frame));
                        bulletObj.x = GameWin.MY_BULLET_VANISH_LOCATION_X;
                        bulletObj.y = GameWin.MY_BULLET_VANISH_LOCATION_Y;
                        this.Frame.removeList.add(bulletObj);
                        this.x = GameWin.ENEMY_VANISH_LOCATION_X;
                        this.y = GameWin.ENEMY_VANISH_LOCATION_Y;
                        this.Frame.removeList.add(this);
                        BacGroundObj.score++;
                    }
                }
            }

    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,ENEMY_1_WIDTH,ENEMY_1_HEIGHT);
    }

    @Override
    public void DeBug() {
        System.out.println("EnemyObj Class");
    }
}
